﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

using System.Data.Entity;
using BO;
namespace UI
{
    using UI.EditForm;
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
#if DEBUG
            //var t = new DesignReportForm();
            //Application.Run(t);
            //return;
#endif
            try
            {
               
                
                //处理未捕获的异常   
                Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
                //处理UI线程异常   
                Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadExceptionEx);
                //处理非UI线程异常   
                AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledExceptionEx);

            
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
            
                var form = new UI.Security.LogonForm();
                if (form.ShowDialog() == DialogResult.OK)
                {
                    GlobalVar.InIt();
                    var app = new AppContext(new AppMainForm());
                    AppContext.Currrent = app;
                    Application.Run(app);
                }
             
            }
            catch (ExceptionEx e)
            {
                ExcptionHandler.ShowExcption(e);
            }
        }

     

   

        static void Application_ThreadExceptionEx(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            string strDateInfo = "出现应用程序未处理的异常：" + DateTime.Now.ToString();
            var error = e.Exception;

            ExcptionHandler.ShowExcption(new ExceptionEx(strDateInfo, error));
            Application.UseWaitCursor = false;
        }


        static void CurrentDomain_UnhandledExceptionEx(object sender, UnhandledExceptionEventArgs e)
        {

            var error = e.ExceptionObject as Exception;
            string strDateInfo = "出现应用程序非UI线程的未处理异常：" + DateTime.Now.ToString();
            ExcptionHandler.ShowExcption(new ExceptionEx(strDateInfo, error));
            Application.UseWaitCursor = false;
        }
    }
}
